﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Ionic.Zip;

namespace DRSLF_robot_
{
    public class Robot
    { 
        public List<Sensor> _Sensors = new List<Sensor>(100);
        public List<String> Sensors
        {
            get
            {
                List<String> str = new List<string>();
                foreach (Sensor sen in _Sensors)
                {
                    if (sen!=null)
                    {
                        str.Add(_Sensors.IndexOf(sen).ToString()+"/"+ sen.ToString());
                    }
                }
                return str;
            }
        }
        //first is motor layer last is highlight layer and others are sensors layers
        private List<Bitmap> robotLayers = new List<Bitmap>(102);
        private Bitmap _bmirobot = new Bitmap(250, 250);
        public Bitmap bmirobot
        {
            get
            {
                _bmirobot = new Bitmap(250, 250);
                Graphics g = Graphics.FromImage(_bmirobot);
                foreach (Bitmap bmi in robotLayers)
                {
                    if (bmi != null)
                    {
                        g.DrawImage(bmi, new Point(0, 0));
                    }
                }
                return _bmirobot;
            }
        }
        private Motor motor;
        

        public void highLightSensor(int index)
        {
            if (index == -1)
                robotLayers[101] = null;
            else
                robotLayers[101] = _Sensors[index].returnSensorHighLight();
        }

        public Robot()
        {
            for (int i = 0; i < 100; i++)
            {
                _Sensors.Add(null);
                robotLayers.Add(null);
            }
            robotLayers.Add(null);
            robotLayers.Add(null);
        }

        public void PlaceMotor(int motorDist, int motorLong, int motorPlace)
        {
            motor = new Motor(motorDist, motorLong, motorPlace);
            robotLayers[0] = motor.returnMotorImage();
        }

        public void PlaceSensor(Size size, Point position, int index)
        {
            Sensor sen = new Sensor(position, size);
            _Sensors[index] = sen;
            robotLayers[index + 1] = sen.returnSensorImage();
        }

        public void RemoveSensor(int index)
        {
            _Sensors[index] = null;
            robotLayers[index + 1] = null;
        }

        public void Export(string Path)
        {
            //Save the robot general settings
            string[] s = new string[2+_Sensors.Count];
                        
            s[0] = motor.MotorLong.ToString() + "M" + motor.MotorDist.ToString() + "M" + motor.MotorPlace.ToString();

            int i = 2;
            foreach (Sensor sen in _Sensors)
            {
                if (sen != null)
                {
                    s[i] = sen.ToPython(_Sensors.IndexOf(sen));
                    i++;
                }
            }
            s[1] = (i-2).ToString();

            File.WriteAllLines("Data.txt", s);
            //********************************

            //Save Robot image
            highLightSensor(-1);
            bmirobot.Save("Image.png", System.Drawing.Imaging.ImageFormat.Png);
            //********************************

            //Add them to an archive
            ZipFile compress = new ZipFile();
            compress.AddFile("Data.txt");
            compress.AddFile("Image.png");
            compress.Save(Path);
        }
    }
}
